import Mock from 'mockjs'
// 使用语法：1.Mock.mock( rurl, function( options ) ) , 2.Mock.mock( rurl, rtype, function( options ) )
import { date } from '@/utils/index.js'
let timestamp = new Date().getTime()

import { lakersData,tableDatas ,roleMsgData,postMsgData} from './userInfo.js'

// 切割数组 做分页
const getNewArray = (array, subGroupLength) => {
	let index = 0;
	let newArray = [];
	while (index < array.length) {
		newArray.push(array.slice(index, (index += subGroupLength)));
	}
	return newArray;
}

// 登录（登录后需获取动态路由）
Mock.mock('/mock/login', 'post', (options) => {

	let par = JSON.parse(options.body)
	let resData = { "error": 0, "msg": "登录成功", "token": "153eae3fe54315062d2151f9d1218f54", "data": { loginCity: '洛杉矶', loginDate: date("Y-m-d", timestamp), "uid": 139620, "mobile": "13886396386", "email": "", "post": "洛杉矶湖人", "number": 23, "gcoin": 36, "jobNum": "025365", "nickName": "勒布朗-詹姆斯", "headimg": "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2Fc53b70c8-241d-46dd-8529-0f58f072fe78%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1705474304&t=62584bd7ae15475ad3688244a82cb07d", position: "前锋", moeny: "4760.735", score: "39298", championsNumber: 4 }, "time": 1620468008 };

	let userData = { "error": 0, "msg": "登录成功", "token": "153eae3fe54315062d2151f9d1218f54", "data": { loginCity: '菲尼克斯', loginDate: date("Y-m-d", timestamp), "uid": 139620, "mobile": "13886396386", "email": "", "post": "菲利克斯太阳", "number": 7, "gcoin": 36, "jobNum": "025365", "nickName": "杜兰特", "headimg": "https://img1.baidu.com/it/u=2222027574,722067708&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750", position: "前锋", moeny: "4760.735", score: "30000", championsNumber: 2 }, "time": 1620468008 };

	let errorRes = { "error": 70001, "msg": "账号或密码错误", "time": 1620467475 }
	if (par.userName === 'admin' && par.password === '123123') {
		return resData
	} else if (par.userName === 'user' && par.password === '123123') {
		return userData
	} else {
		return errorRes
	}
})
// 登出
Mock.mock('/mock/logout', 'post', (options) => {

	let resData = {
		error: 0,
		msg: 'ok',
		data: '退出成功',
		time: 1620466240,
	}
	return resData
})
// 获取动态路由（模拟接口，根据options.body参数返回不同路由）
Mock.mock('/mock/getPermissionMenu', 'post', (options) => {
	let par = JSON.parse(options.body)
	console.log(par);
	let adminData = {
		error: 0,
		msg: 'ok',
		time: 1620466240,
		data: [
			{
				icon: "el-icon-lx-home",
				path: "/dashboard",
				title: "系统首页",
			},
			{
				icon: "el-icon-lx-cascades",
				path: "/baseTable",
				title: "基础表格",
			},
			{
				icon: "el-icon-lx-copy",
				path: "/tabs",
				title: "tab选项卡",
			},
			{
				icon: "el-icon-lx-punch",
				path: "/org",
				title: "组织机构",
				children: [
					{
						path: "/org/demo/index",
						title: "组织机构逻辑",
					},
					{
						path: "/org/userdepManage/index",
						title: "用户部门管理",
					},
					{
						path: "/org/menuManage/index",
						title: "菜单管理",
					},
					{
						path: "/org/roleManage/index",
						title: "角色管理",
					},
					{
						path: "/org/postManage/index",
						title: "岗位管理",
					},
				],
			},
			{
				icon: "el-icon-lx-calendar",
				path: "/form",
				title: "表单相关",
				children: [
					{
						path: "/form/baseform",
						title: "基本表单",
					},
					{
						path: "/form-2",
						title: "三级菜单",
						children: [
							{
								path: "/form/form-2/editor",
								title: "富文本编辑器",
							},
							{
								path: "/form/form-2/markdown",
								title: "markdown编辑器",
							},
						],
					},
					{
						path: "/form/upload",
						title: "文件上传",
					},
				],
			},
			{
				icon: "el-icon-lx-emoji",
				path: "/icon",
				title: "自定义图标",
			},
			{
				icon: "el-icon-lx-rank",
				path: "/baseCharts",
				title: "schart图表",
			},
			{
				icon: "el-icon-lx-global",
				path: "/i18n",
				title: "国际化功能",
			},
			{
				icon: "el-icon-lx-warn",
				path: "/error",
				title: "错误处理",
				children: [
					{
						path: "/error/403",
						title: "403页面",
					},
					{
						path: "/error/404",
						title: "404页面",
					},
				],
			},
		],
	}
	let userData = {
		error: 0,
		msg: 'ok',
		time: 1620466240,
		data: [
			{
				icon: 'el-icon-lx-home',
				path: '/dashboard',
				title: '系统首页'
			},
			{
				icon: 'el-icon-lx-cascades',
				path: '/baseTable',
				title: '基础表格'
			},
			{
				icon: 'el-icon-lx-global',
				path: '/i18n',
				title: '国际化功能'
			},
			{
				icon: 'el-icon-lx-warn',
				path: '/error',
				title: '错误处理',
				children: [
					{
						path: '/error/403',
						title: '403页面'
					},
					{
						path: '/error/404',
						title: '404页面'
					}
				]
			},

		],
	}
	if (par.userName === 'admin') {
		return adminData
	} else {
		return userData
	}
})

// 获取表格数据
Mock.mock('/mock/getTableData', 'post', (options) => {

	const { pageIndex, pageSize } = JSON.parse(options.body)

	let newArray = getNewArray(tableDatas, pageSize)

	let tableData = {
		error: 0,
		msg: 'ok',
		time: 1620466240,
		data: {
			'list': newArray[pageIndex-1],
			'pageTotal': tableDatas.length,
		}
	}
	return tableData
})


// 获取role数据
Mock.mock('/mock/getRoleMsgData', 'get', (options) => {

	let RoleMsgData = {
		error: 0,
		msg: 'ok',
		time: 1620466240,
		data: roleMsgData
	}
	return RoleMsgData
})


// 获取表post数据
Mock.mock('/mock/getPostMsgData', 'get', (options) => {

	let PostMsgData = {
		error: 0,
		msg: 'ok',
		time: 1620466240,
		data: postMsgData
	}
	return PostMsgData
})


let dataList = [...lakersData]

Mock.mock('/mock/getUserdepData', 'post', (options) => {
	const par = options.body
	if(par) {
		dataList =lakersData.filter(item => {
			return item.post == par
		}) 
	}
	
	let tableData = {
		error: 0,
		msg: 'ok',
		time: 1620466240,
		data: {
			'list': dataList,
			'pageTotal':dataList.length,
		}
	}
	return tableData
})


// 更新岗位信息
Mock.mock('/mock/updataPostMsg', 'post', (options) => {
	const {level,transferList} = JSON.parse(options.body)
 
 transferList.forEach(item => {
	dataList = lakersData.map(i => i.name == item?{...i,post:level } :i )
	 })
	let tableData = {
		error: 0,
		msg: 'ok',
		time: 1620466240,
		data: dataList
	}
	return tableData
})
